﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.supply
{
    public partial class AddEquipmentMange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = new lcs.BLL.user().GetList(" roles_id = 3 ");//物料员

                this.ddl_clyUser.DataSource = ds;
                this.ddl_clyUser.DataTextField = "user_name";
                this.ddl_clyUser.DataValueField = "user_id";
                this.ddl_clyUser.DataBind();

                this.ddl_clyUser.Items.Insert(0, new ListItem("---未选择---","-1"));

                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int id = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(id);
                }
            }
        }

        private void ShowInfo(int id)
        {
            lcs.BLL.equipmentManage bll = new lcs.BLL.equipmentManage();
            lcs.Model.equipmentManage model = bll.GetModel(id);
            this.lblid.Text = model.id.ToString();
            this.txtequipmentManageName.Text = model.equipmentManageName;
            //this.txtequipmentManageName.ReadOnly = true;
            
            this.ddl_clyUser.SelectedValue = model.cly_userId.ToString();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtequipmentManageName.Text.Trim().Length == 0)
            {
                strErr += "设备管理单位名称不能为空！\\n";
            }
            if (this.ddl_clyUser.SelectedValue == "-1")
            {
                strErr += "请选择物料员";
            }

            if (strErr != "")
            {
                Maticsoft.Common.MessageBox.Show(this, strErr);
                return;
            }
            string equipmentManageName = this.txtequipmentManageName.Text;
            int cly_userId = int.Parse(this.ddl_clyUser.SelectedValue);

            lcs.Model.equipmentManage model = new lcs.Model.equipmentManage();
            model.equipmentManageName = equipmentManageName;
            model.cly_userId = cly_userId;

            lcs.BLL.equipmentManage bll = new lcs.BLL.equipmentManage();
          
            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                bll.Update(model);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListEquipmentMange.aspx");

            }
            else
            {
                try
                {
                    bll.Add(model);
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListEquipmentMange.aspx");

                }
                catch (Exception)
                {

                    Maticsoft.Common.MessageBox.Show(this, "管理单位名称已存在，不能重复添加！");

                   
                }
              
            }
        }
    }
}